package com.xbstar.tools;


import java.math.BigInteger;
import java.security.MessageDigest;
import java.util.Date;
import java.util.HashMap;

import org.json.JSONObject;


import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public  class JFUserAuthentication{

public static JSONObject getAticket(String loginkey){
							//生成ticket
							 String lk=loginkey;
							 Date now=new Date();
							 String time=now.getTime()+"";
							 String appsec="f1de040b4e7d8b54ac4e33452befab6d";
							 String[] strSet = new String[] { lk, time, appsec };
								java.util.Arrays.sort(strSet);
								String keys = "";
								for (String string : strSet) {
									keys = keys + string;
								}
								String pwdtoken = JFUserAuthentication.sha1(keys);;
								JSONObject tjson=new JSONObject();
								tjson.put("time",time);
								tjson.put("token",pwdtoken);
								tjson.put("loginkey",lk);
							return	tjson;								
}
	
	/**
	 * BASE64解密
	 * 
	 * @param key
	 * @return
	 * @throws Exception
	 */
	public static String decryptBASE64(String key) throws Exception {
		
		return new String((new BASE64Decoder()).decodeBuffer(key),"UTF-8");
	}

	/**
	 * BASE64加密
	 * 
	 * @param key
	 * @return
	 * @throws Exception
	 */
	public static String encryptBASE64(String key) throws Exception {
		return (new BASE64Encoder()).encodeBuffer(key.getBytes("UTF8"));
	}
	/**
	 * 实现验证信息sh1加密
	 * @param key
	 * @return 16位明文
	 */
	private static String sha1(String key) {
		try {
			MessageDigest md = MessageDigest.getInstance("SHA1");
			md.update(key.getBytes());
			String pwd = new BigInteger(1, md.digest()).toString(16);
			return pwd;
		} catch (Exception e) {
			e.printStackTrace();
			return key;
		}
	}
	
/**
 * 获取用户标识
 * @param request 此次请求
 * @return 存在返回字符串，不存在返回空字符串
 */
	public static String getLoginKeyFromRequest(String ticket) {
		JSONObject json=new JSONObject(ticket);
	     String time=json.getString("time");
	    String  loginkey= json.getString("loginkey");
	    String token=json.getString("token");
	    String APPToken="18530770503";
	    if (loginkey != null && time != null && token != null ) {
			String[] strSet = new String[] { loginkey, time, APPToken };
			java.util.Arrays.sort(strSet);
			String key = "";
			for (String string : strSet) {
				key = key + string;
			}
			String pwd = JFUserAuthentication.sha1(key);
			System.out.println("加密后明文是："+pwd);
			if(pwd.equals(token)){
				return loginkey;
			}else{
				return null;
			}
		}else {
			return null;
		}
	}
	public static Boolean isRightUser(String ticket) {
		JSONObject json=new JSONObject(ticket);
	     String time=json.getString("time");
	    String  loginkey= json.getString("loginkey");
	    String token=json.getString("token");
	    String APPToken="18530770503";
	    if (loginkey != null && time != null && token != null ) {
			String[] strSet = new String[] { loginkey, time, APPToken };
			java.util.Arrays.sort(strSet);
			String key = "";
			for (String string : strSet) {
				key = key + string;
			}
			String pwd = JFUserAuthentication.sha1(key);
			System.out.println("加密后明文是："+pwd);
			if(pwd.equals(token)){
				return true;
			}else{
				return false;
			}
		}else {
			return false;
		}
	}
	}
